class Solution:

    def __init__(self):
        self.cache = set()

    def isHappy(self, n: int) -> bool:
        return self.recursion(n)

    def recursion(self, n):
        if n in self.cache:
            return False
        result = 0
        for i in map(int, str(n)):
            result += i ** 2

        if result == 1:
            return True
        self.cache.add(result)
        return self.recursion(result)


print(Solution().isHappy(2))
print(Solution().isHappy(18))
